/**
 * huize Service Inc
 * All Rights Reserved @2018
 */
package com.huize.zhike.framework.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * 描述:
 *
 * @author tianyuliang
 * @version $Id: ZoneDatetimeUtils.java, v0.1 2020/5/3
 */
public class ZoneDatetimeUtils extends DatetimeUtils {

    public static final TimeZone CHINA_TIME_ZONE = TimeZone.getTimeZone("GMT+8:00");

    public static final TimeZone UTC_TIME_ZONE = TimeZone.getTimeZone("UTC");

    /**
     * 标准字符串(yyyy-MM-dd HH:mm:ss)转化为时区时间
     *
     * @param strTime
     * @return
     */
    public static Date parseZoneDatetime(String strTime) {
        return parseZoneCustomDate(strTime, PATTERN_TIME);
    }

    /**
     * 标准字符串(yyyy-MM-dd)转化为时区日期
     *
     * @param strZoneDate
     * @return
     */
    public static Date parseZoneDate(String strZoneDate) {
        return parseZoneCustomDate(strZoneDate, PATTERN_DATE);
    }

    /**
     * 中国标准时间转化为时区时间
     *
     * @param strZoneDate
     * @param formatter
     * @return
     */
    public static Date parseZoneCustomDate(String strZoneDate, String formatter) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(formatter);
            df.setTimeZone(CHINA_TIME_ZONE);
            Date zoneDate = df.parse(strZoneDate);
            return zoneDate;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 时区时间转化为中国标准时间
     *
     * @param strZoneDate
     * @param formatter
     * @return
     */
    public static Date parseChinaCustomTime(String strZoneDate, String formatter) {
        try {
            SimpleDateFormat df = new SimpleDateFormat(formatter);
            df.setTimeZone(UTC_TIME_ZONE);
            Date chinaDate = df.parse(strZoneDate);
            return chinaDate;
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 时区时间格式化为任意时间
     *
     * @param zoneDate
     * @param formatter
     * @return
     */
    public static String formatZoneCustomDate(Date zoneDate, String formatter) {
        SimpleDateFormat df = new SimpleDateFormat(formatter);
        df.setTimeZone(UTC_TIME_ZONE);
        String zoneValue = df.format(zoneDate);
        return zoneValue;
    }

    /**
     * 将带有时区的日期转化为时区字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @param zoneDate
     * @return
     */
    public static String formatZoneDatetime(Date zoneDate) {
        return formatZoneCustomDate(zoneDate, PATTERN_TIME);
    }

    /**
     * 将带有时区的日期转化为时区字符串(yyyy-MM-dd)
     *
     * @param zoneDate
     * @return
     */
    public static String formatZoneDate(Date zoneDate) {
        return formatZoneCustomDate(zoneDate, PATTERN_DATE);
    }

    /**
     * 标准日期字符串(yyyy-MM-dd HH:mm:ss)转化为时区字符串(yyyy-MM-dd HH:mm:ss )
     *
     * @param strDatetime
     * @return
     */
    public static String convertChinaTimeToUtcTime(String strDatetime) {
        Date zoneDatetime = parseZoneDatetime(strDatetime);
        return formatZoneDatetime(zoneDatetime);
    }

    /**
     * 标准日期字符串(yyyy-MM-dd)转化为时区字符串(yyyy-MM-dd)
     *
     * @param strZoneDate
     * @return
     */
    public static String convertChinaDateToUtcDate(String strZoneDate) {
        Date zoneDate = parseZoneDate(strZoneDate);
        return formatZoneDate(zoneDate);
    }

    /**
     * 时区字符串(yyyy-MM-dd HH:mm:ss) 转化为标准日期字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @param utcTime
     * @return
     */
    public static String convertUtcTimeToChinaTime(String utcTime) {
        Date chinaDate = parseChinaCustomTime(utcTime, PATTERN_TIME);
        return convertUtcToChina(chinaDate, PATTERN_TIME);
    }

    /**
     * 时区字符串(yyyy-MM-dd HH:mm:ss) 转化为指定格式的formatter字符串
     *
     * @param utcTime
     * @return
     */
    public static String convertUtcTimeToChinaTime(String utcTime, String formatter) {
        Date chinaDate = parseChinaCustomTime(utcTime, formatter);
        return convertUtcToChina(chinaDate, formatter);
    }

    /**
     * 时区字符串(yyyy-MM-dd) 转化为标准日期字符串(yyyy-MM-dd)
     *
     * @param utcTime
     * @return
     */
    public static String convertUtcDateToChinaDate(String utcTime) {
        Date chinaDate = parseChinaCustomTime(utcTime, PATTERN_DATE);
        return convertUtcToChina(chinaDate, PATTERN_DATE);
    }

    /**
     * 时区字符串(yyyy-MM-dd) 转化为标准日期字符串(yyyy-MM-dd)
     *
     * @param utcTime
     * @return
     */
    public static String convertUtcDatetimeToChinaDate(String utcTime) {
        Date chinaDate = parseChinaCustomTime(utcTime, PATTERN_TIME);
        return convertUtcToChina(chinaDate, PATTERN_DATE);
    }

    /**
     * 时区字符串(yyyy-MM-dd HH:mm:ss) 转化为标准日期字符串(yyyy-MM-dd HH:mm:ss)
     *
     * @param chinaDate
     * @return
     */
    public static String convertUtcToChina(Date chinaDate, String formatter) {
        SimpleDateFormat df = new SimpleDateFormat(formatter);
        df.setTimeZone(CHINA_TIME_ZONE);
        String chinaValue = df.format(chinaDate);
        return chinaValue;
    }


    public static void main(String[] args) {
        String input_a = "2020-05-03 15:34:32";
        String output_a = convertChinaTimeToUtcTime(input_a);
        System.out.println("input_a=" + input_a + ", output_a=" + output_a);

        String input_zone_aaaa = "2020-05-03 15:34:32";
        String output_aaaa = convertUtcTimeToChinaTime(input_zone_aaaa);
        System.out.println("input_zone_aaaa=" + input_zone_aaaa + ", output_aaaa=" + output_aaaa);

        String input_ab = "2020-05-03";
        String output_ab = convertChinaDateToUtcDate(input_ab);
        System.out.println("input_ab=" + input_ab + ", output_ab=" + output_ab);

        String input_zone_abcc = "2020-05-03";
        String output_abcc = convertUtcDateToChinaDate(input_zone_abcc);
        System.out.println("input_zone_abcc=" + input_zone_abcc + ", output_abcc=" + output_abcc);

        String input_zone_abccddd = "2020-05-03 00:00:00";
        String output_abccdddd = convertUtcDatetimeToChinaDate(input_zone_abccddd);
        System.out.println("input_zone_abccddd=" + input_zone_abccddd + ", output_abccdddd=" + output_abccdddd);

    }

}
